/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehouse.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.warehouse.entity.Material;
import com.thinkgem.jeesite.modules.warehouse.entity.MaterialRecord;
import com.thinkgem.jeesite.modules.warehouse.service.MaterialRecordService;
import com.thinkgem.jeesite.modules.warehouse.service.MaterialService;

/**
 * MaterialRecordController
 * 
 * @author qrg
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouse/materialRecord")
public class MaterialRecordController extends BaseController {

	@Autowired
	private MaterialRecordService materialRecordService;

	@Autowired
	private MaterialService materialService;

	@ModelAttribute
	public MaterialRecord get(@RequestParam(required = false) String id) {
		MaterialRecord entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = materialRecordService.get(id);
		}
		if (entity == null) {
			entity = new MaterialRecord();
		}
		return entity;
	}

	@RequiresPermissions("warehouse:materialRecord:view")
	@RequestMapping(value = { "list", "" })
	public String list(MaterialRecord materialRecord, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<MaterialRecord> page = materialRecordService.findPage(new Page<MaterialRecord>(request, response),
				materialRecord);
		model.addAttribute("page", page);
		return "modules/warehouse/materialRecordList";
	}

	@RequiresPermissions("warehouse:materialRecord:view")
	@RequestMapping(value = "form")
	public String form(MaterialRecord materialRecord, Model model) {

		//List<Material> materials = materialService.findList(new Material());
		List<Material> materials = materialService.findAllByOffice(UserUtils.getUser().getOffice());

		model.addAttribute("materials", materials);
		model.addAttribute("materialRecord", materialRecord);
		return "modules/warehouse/materialRecordForm";
	}

	@RequiresPermissions("warehouse:materialRecord:edit")
	@RequestMapping(value = "save")
	public String save(MaterialRecord materialRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, materialRecord)) {
			return form(materialRecord, model);
		}
		materialRecordService.save(materialRecord);
		addMessage(redirectAttributes, "保存MaterialRecord成功");
		return "redirect:" + Global.getAdminPath() + "/warehouse/materialRecord/?repage";
	}

	@RequiresPermissions("warehouse:materialRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(MaterialRecord materialRecord, RedirectAttributes redirectAttributes) {
		materialRecordService.delete(materialRecord);
		addMessage(redirectAttributes, "删除MaterialRecord成功");
		return "redirect:" + Global.getAdminPath() + "/warehouse/materialRecord/?repage";
	}

}